package com.guofen.service.shlj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.ShljDeodorizationtowerWaterChange1Mapper;
import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.pojo.ShljDeodorizationtowerWaterChange1;
import com.guofen.service.shlj.DeodorizationtowerWaterChange1Service;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeodorizationtowerWaterChange1ServiceImpl implements DeodorizationtowerWaterChange1Service {
    @Autowired
    private ShljDeodorizationtowerWaterChange1Mapper shljDeodorizationtowerWaterChange1Mapper;

    //开始放水
    @Override
    public boolean beginReducingWater(String water_reduce_time, String recording_person) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();

        ShljDeodorizationtowerWaterChange1 shljDeodorizationtowerWaterChange1 = new ShljDeodorizationtowerWaterChange1();
        shljDeodorizationtowerWaterChange1.setDate(date);
        shljDeodorizationtowerWaterChange1.setWaterReduceTime(water_reduce_time);
        shljDeodorizationtowerWaterChange1.setRecordingPerson(recording_person);
        int result = shljDeodorizationtowerWaterChange1Mapper.insert(shljDeodorizationtowerWaterChange1);
        if (result == 1)
            return true;
        else
            return false;
    }

    //开始加水
    @Override
    public boolean beginAddingWater(String water_add_time, Double water_add, Double deodorant, Double freshener) {
        ShljDeodorizationtowerWaterChange1 shljDeodorizationtowerWaterChange = shljDeodorizationtowerWaterChange1Mapper.selectLast();
        shljDeodorizationtowerWaterChange.setWaterAddTime(water_add_time);
        shljDeodorizationtowerWaterChange.setWaterAddition(water_add);
        shljDeodorizationtowerWaterChange.setDeodorant(deodorant);
        shljDeodorizationtowerWaterChange.setFreshener(freshener);
        int result = shljDeodorizationtowerWaterChange1Mapper.updateByPrimaryKeySelective(shljDeodorizationtowerWaterChange);
        if (result == 1)
            return true;
        else
            return false;
    }

    //开始清理填料
    @Override
    public boolean beginCleaningStuffing(String clean_filler_time, String filler_end_time, Integer clean_nozzle_number) {
        ShljDeodorizationtowerWaterChange1 shljDeodorizationtowerWaterChange = shljDeodorizationtowerWaterChange1Mapper.selectLast();
        shljDeodorizationtowerWaterChange.setCleanFillerTime(clean_filler_time);
        shljDeodorizationtowerWaterChange.setFillerEndTime(filler_end_time);
        shljDeodorizationtowerWaterChange.setCleanNozzleNumber(clean_nozzle_number);
        int result = shljDeodorizationtowerWaterChange1Mapper.updateByPrimaryKeySelective(shljDeodorizationtowerWaterChange);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public ShljDeodorizationtowerWaterChange1 findForm() {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        System.out.println("当前日期" + date);
        //获取最新记录
        ShljDeodorizationtowerWaterChange1 shljDeodorizationtowerWaterChange = shljDeodorizationtowerWaterChange1Mapper.findLast();
        System.out.println("最新记录" + shljDeodorizationtowerWaterChange1Mapper.findLast());
        //获取最新记录日期
        String dateLast =  shljDeodorizationtowerWaterChange.getDate();
        System.out.println("最新记录日期" + dateLast);

        if (date.equals(dateLast))
            return shljDeodorizationtowerWaterChange;
        else
            return null;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<ShljDeodorizationtowerWaterChange1> list = shljDeodorizationtowerWaterChange1Mapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<ShljDeodorizationtowerWaterChange1> list = shljDeodorizationtowerWaterChange1Mapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return shljDeodorizationtowerWaterChange1Mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(ShljDeodorizationtowerWaterChange1 record) {
        return shljDeodorizationtowerWaterChange1Mapper.updateByPrimaryKeySelective(record);
    }
}
